package com.fortune.service.impl;

import com.fortune.dao.CReturnDepositDao;
import com.fortune.dto.in.cReturnDeposit.CReturnAddDepositDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.CReturnDepositService;
import com.fortune.vo.cReturnDeposit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2018/5/29 0029.
 */
@Service
public class CReturnDepositServiceImpl implements CReturnDepositService {
    @Autowired
    private CReturnDepositDao dao;

    @Override
    public List<CReturnDeposit> findCReturnDepositList(Integer type, String mid, String did, String id) {
        return dao.findCReturnDepositList(type,mid,did,id);
    }

    @Override
    public List<RefundDetails> findCReturnProList(String id) {
        return dao.findCReturnProList(id);
    }

    @Override
    public CReturnDepositDetail findCReturnDepositDetail(String id, String clientId,String mid,String did) {
        CReturnDepositDetail cReturnDepositDetail = dao.findCReturnDepositDetail(id,clientId,mid,did);
        if(cReturnDepositDetail.getIsAllow().equals("YES") && cReturnDepositDetail.getIsReturn().equals("YES") && cReturnDepositDetail.getIsAccount().equals("YES") && cReturnDepositDetail.getBoxIsIn().equals("YES")){
                cReturnDepositDetail.setcRmessage("空箱已入库！");
        }else if(cReturnDepositDetail.getIsAllow().equals("YES") && cReturnDepositDetail.getIsReturn().equals("NO") && cReturnDepositDetail.getIsAccount().equals("NO") && cReturnDepositDetail.getBoxIsIn().equals("NO")){
            cReturnDepositDetail.setcRmessage("退押金工单已批准,司机前往接货！");
        }else if(cReturnDepositDetail.getIsAllow().equals("YES") && cReturnDepositDetail.getIsReturn().equals("YES") && cReturnDepositDetail.getIsAccount().equals("NO") && cReturnDepositDetail.getBoxIsIn().equals("NO")){
            cReturnDepositDetail.setcRmessage("司机已接货完成,等待空箱入库！");
        }else if(cReturnDepositDetail.getIsAllow().equals("YES") && cReturnDepositDetail.getIsReturn().equals("YES") && cReturnDepositDetail.getIsAccount().equals("YES") && cReturnDepositDetail.getBoxIsIn().equals("NO")){
            cReturnDepositDetail.setcRmessage("司机已接货完成,等待空箱入库！");
        }else{
            cReturnDepositDetail.setcRmessage("退押金工单已提交,等待内勤处理！");
        }
        List<DetailCReturn> list = dao.findListCReturn(cReturnDepositDetail.getId(),clientId,mid,did);
        cReturnDepositDetail.setList(list);
        return cReturnDepositDetail;
    }

    @Override
    public VReturnDepositOverview findVReturnDepositOverview(String mid,String did,String cid) {
        VReturnDepositOverview vReturnDepositOverview = dao.findVReturnDepositOverview(mid,did,cid);
        if(vReturnDepositOverview == null){
            vReturnDepositOverview.setAllow(0);
            vReturnDepositOverview.setComplete(0);
            vReturnDepositOverview.setIn(0);
            vReturnDepositOverview.setUnhandle(0);
        }
        return vReturnDepositOverview;
    }

    @Override
    public int deleteReturnDeposit(String id,String mid,String did,String cid) {
        return dao.deleteReturnDeposit(id,mid,did,cid);
    }

    @Override
    @Transactional
    public int apply(CReturnAddDepositDto dto, String mid, String did, String cid,String id) {
        int num = dao.apply(dto,mid,did,cid,id);
        if(num > 0){
          List<RefundDetails> list = dto.getRefundDetails();
          for(RefundDetails refundDetails : list){
              int no = dao.findNO(mid,did,cid);
              if(no == 0){
                  no = 1;
              }else{
                  no = no + 1;
              }
              int success = dao.applyDetail(refundDetails,mid,did,cid,id,no);
              if(success <= 0){
                  throw new InsertInnerErrorException("添加失败");
              }
          }
          return 1;
        }else{
            return 0;
        }
    }
}
